package com.telit.firecommand.strategy.context;

import com.telit.common.entity.DataReturn;
import com.telit.firecommand.strategy.FireResourceStrategyInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Component
public class FireResourceStrategyContext {

    private final Map<String, FireResourceStrategyInterface> strategyMap = new ConcurrentHashMap<>();

    @Autowired
    public FireResourceStrategyContext(Map<String, FireResourceStrategyInterface> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach(this.strategyMap::put);
    }

    public void excelTemplate(String beanName, HttpServletResponse response) throws Exception {
        if (StringUtils.isNotBlank(beanName)) {
            strategyMap.get(beanName).excelTemplate(response);
        }
    }

    public void excelImport(String beanName, MultipartFile file) throws Exception {
        if (StringUtils.isNotBlank(beanName)) {
            strategyMap.get(beanName).excelImport(file);
        }
    }

    public void excelExport(String beanName,Map<String,Object> queryMap,HttpServletResponse response){
        if (StringUtils.isNotBlank(beanName)) {
            strategyMap.get(beanName).excelExport(queryMap,response);
        }
    }

    public DataReturn selectResourceList(String beanName,Map<String,Object> queryMap){
        if(StringUtils.isNotBlank(beanName)){
            return strategyMap.get(beanName).selectResourceList(queryMap);
        }
        return DataReturn.ok();
    }
}
